using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace CodePlex.Resourceful.Amazon.S3
{

    /*
  <Contents>
    <Key>Neo</Key>
    <LastModified>2005-11-17T07:13:48Z</LastModified>
    <ETag>&quot;828ef3fdfa96f00ad9f27c383fc9ac7f&quot;</ETag>
    <Size>4</Size>
    <StorageClass>STANDARD</StorageClass>
     <Owner>
      <ID>bcaf1ffd86f41caff1a493dc2ad8c2c281e37522a640e161ca5fb16fd081034f</ID>
      <DisplayName>webfile</DisplayName>
    </Owner>
 </Contents>
     */


    public class Key
    {

        private readonly string _name;
        private readonly DateTime _lastModified;
        private readonly string _etag;
        private readonly long _size;
        private readonly string _storageClass;
        private readonly ACLPrincipal _owner;

        public Key(string name, DateTime lastModified, string etag, long size, string storageClass, ACLPrincipal owner)
        {
            _name = name;
            _lastModified = lastModified;
            _etag = etag;
            _size = size;
            _storageClass = storageClass;
            _owner = owner;
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public DateTime LastModified
        {
            get
            {
                return _lastModified;
            }
        }

        public long Size
        {
            get
            {
                return _size;
            }
        }

        public string StorageClass
        {
            get
            {
                return _storageClass;
            }
        }

        public string Etag
        {
            get
            {
                return _etag;
            }
        }

        public ACLPrincipal Owner
        {
            get
            {
                return _owner;
            }
        }


        public static Key Parse(XmlReader reader)
        {
            string key = null;
            DateTime lastModified= DateTime.MinValue;
            string etag= null;
            long size= int.MinValue;
            string storageClass= null;
            ACLPrincipal owner = null;


            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "Key")
                    {
                        key = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "LastModified")
                    {
                        string rdr = Utils.ReadString(reader);
                        lastModified = DateTime.Parse(rdr);
                    }
                    else if (reader.Name == "ETag")
                    {
                        etag = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "Size")
                    {
                        size = long.Parse(Utils.ReadString(reader));
                    }
                    else if (reader.Name == "StorageClass")
                    {
                        storageClass = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "Owner")
                    {
                        owner = S3Connection.ParseACLPrincipal(reader, "Owner");
                    }
                }

                if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Contents")
                {
                    return new Key(key, lastModified, etag, size, storageClass, owner);
                }

            }

            throw CodePlexException.Format("Failed to parse contents.");

        }
    }
}
